import dayjs from 'dayjs';
import { IFormateDateTimeOpts } from './types';

/**
 * @description 今天与指定日期的天数差
 * @param string date 
 * @returns number
 */
export function getDaysSince(date: string): number {
    const now = dayjs()
    const diff = now.diff(dayjs(date), 'day')
    return diff
}

export function formatDateTime(dateTime: string, options: IFormateDateTimeOpts = {
    dateTimeFormat: 'YYYY-MM-DD HH:mm:ss',
    date: true,
    time: true
}): string {
    let formatStr = '';
    if (options.date && options.time) {
        formatStr += options.dateTimeFormat
    } else if (options.date) {
        formatStr += options.dateTimeFormat.split(' ')[0]
    } else if (options.time) {
        formatStr += options.dateTimeFormat.split(' ')[1]
    }

    return dayjs(dateTime).format(formatStr)
}